<?php

class Page extends AModel
{
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @return Place
	 */
	protected static $_instance = NULL;
	public static function i()
	{
		return NULL == self::$_instance ? self::$_instance = new self() : self::$_instance;
	}

	// ------------------------------------------------------------------------------------
	/**
	 * @param array $attributes
	 * @return void
	 */
	public function __construct($attributes = array())
	{
		$this->_tableName = Yii::app()->params['TABLES']['pages'];
		$this->_tableColumnPrefix = 'page_';

		parent::__construct($attributes);

		if(!empty($this->_attributes['page_title']))
		{
			$this->_attributes['page_title'] = htmlspecialchars($this->_attributes['page_title'], ENT_QUOTES);
		}
	}

	// ------------------------------------------------------------------------------------
	/**
	 * @return array
	 */
	public function attributeNames()
	{
		return array(
			'page_title' => 'Название',
			'page_priority' => 'Приоритет',
		);
	}

	// ------------------------------------------------------------------------------------
	/**
	 * @return array
	 */
	public function rules()
	{
		return array(
			array('page_priority', 'numerical'),
			array('page_title', 'safe'),
			array('page_name', 'safe'), array('page_name', 'required'),
			//array('page_content', 'safe'),
			array('page_content_src', 'safe'),
			array('page_hidden', 'boolean'),
		);
	}

	// ------------------------------------------------------------------------------------
	/**
	 * @return bool
	 */
	protected function beforeSave()
	{
		if(empty($this->page_name))
		{
			$this->page_name = StringHelper::translit($this->page_title);
		}

		if(!empty($this->page_content_src))
		{
			$str = $this->page_content_src;

			$replacement = array(
				'<' => '&lt;',
				'>' => '&gt;',
			);
			$str = str_replace(array_keys($replacement), $replacement, $str);

			$replacement = array(
				'/\[h1\](.*?)\[\/h1\]/i' => '<h1>$1</h1>',
				'/\[h2\](.*?)\[\/h2\]/i' => '<h2>$1</h2>',
				'/\[h3\](.*?)\[\/h3\]/i' => '<h3>$1</h3>',
				'/\[p\](.*?)\[\/p\]/i'   =>  '<p>$1</p>',

				'/\[b\](.*?)\[\/b\]/i'   =>  '<b>$1</b>',
				'/\[i\](.*?)\[\/i\]/i'   =>  '<i>$1</i>',

				'/\[ol\](.*?)\[\/ol\]/si' => '<ol>$1</ol>',
				'/\[ul\](.*?)\[\/ul\]/si' => '<ul>$1</ul>',
				'/\[ul=images\](.*?)\[\/ul\]/si' => '<ul class="ul-images">$1</ul>',

				'/\[li\](.*?)\[\/li\]/si' => '<li>$1</li>',

				'/\[table=([^\]]+)\](.*?)\[\/table\]/si' => '<table class="t-$1">$2</table>',

				'/\[url=([^\]]+)\](.*?)\[\/url\]/i' => '<a href="$1">$2</a>',
				'/\[url\](.*?)\[\/url\]/i' => '<a href="$1">$1</a>',

				'/\[spoiler=([^\]]+)\]/i' => '<div class="b-spoiler"><div class="b-spoiler-header">$1</div><div class="b-spoiler-content">',
				'/\[\/spoiler\]/i' => '</div></div>',

				'/\[br\s?\/?\]/i' => '<br />',
				'/\[hr\s?\/?\]/i' => '<hr />',

				'/\[img\](.*?)\[\/img\]/i' => '<img src="/$1" alt="" />',
				
				'/\[photo\](.*?)\[\/photo\]/i' => '<img class="i-photo" src="/photos/$1.jpg" alt="" />',
				'/\[photo=(left|center|right)\](.*?)\[\/photo\]/i' => '<img class="i-photo i-photo-$1" src="/photos/$2.jpg" alt="" />',

				'/\[url=([^\]]+)\](.*?)\[\/url\]/i' => '<a href="$1">$2</a>',
				'/\[url\](.*?)\[\/url\]/i' => '<a href="$1">$1</a>',

				'/\[xpage=([^\]]+)\](.*?)\[\/xpage\]/i' => '<a class="x-hash" xhash="w:$1" href="/$1/">$2</a>',

				'/\[xphoto\]([^\/]*)\/?(.*?)\[\/xphoto\]/i' => '<a class="x-hash" xhash="p:$1-$2.jpg" href="/photos/$1/$2.jpg"><img class="i-photo" src="/photos/$1/$2-icon.jpg" width="150" height="113" alt="" /></a>',
				'/\[xphoto=(left|center|right)\]([^\/]*)\/?(.*?)\[\/xphoto\]/i' => '<a class="x-hash" xhash="p:$2-$3.jpg" href="/photos/$2/$3.jpg"><img class="i-photo i-photo-$1" src="/photos/$2/$3-icon.jpg" width="150" height="113" alt="" /></a>',

				'/\[contacts=form\]/i' =>
'<form id="feed_form" method="post" action="">
	<div id="feed_message" class="b-message"></div>
	<dl>
		<dt>Имя:</dt>
		<dd><input type="text" class="i-text" name="name" value="" /></dd>
		<dt>Почта:</dt>
		<dd><input type="text" class="i-text" name="mail" value="" /></dd>
		<dt>Текст сообщения:</dt>
		<dd><textarea name="message"></textarea></dd>
		<dd><input type="submit" class="i-submit" value="Отправить сообщение" /></dd>
	</dl>
</form>',

				'/\[contacts=phone\]/i' => '<b id="phone">+38 (***) ***-**-**</b>',
				'/\[contacts=mail\]/i' => '<b><a id="mail" href="mailto:***@***.***">***@***.***</a></b>',
			);

			$str = preg_replace(array_keys($replacement), $replacement, $str);


			if(!function_exists('replaceRowCallback'))
			{
				function replaceRowCallback($matches)
				{
					return '<tr><td>' . implode('</td><td>', explode('[|]', $matches[1])) . '</td></tr>';
				}
			}
			$str = preg_replace_callback('/\[row](.*?)\[\/row]/i', 'replaceRowCallback', $str);


			if(!function_exists('replaceHRowCallback'))
			{
				function replaceHRowCallback($matches)
				{
					return '<tr><th>' . implode('</th><th>', explode('[|]', $matches[1])) . '</th></tr>';
				}
			}
			$str = preg_replace_callback('/\[hrow](.*?)\[\/hrow]/i', 'replaceHRowCallback', $str);


			if(!function_exists('replaceRowXCallback'))
			{
				function replaceRowXCallback($matches)
				{
					return '<tr><td colspan="' . $matches[2] . '">' . implode('</td><td colspan="' . $matches[2] . '">', explode('[|]', $matches[3])) . '</td></tr>';
				}
			}
			$str = preg_replace_callback('/\[row=((\\d+)x)](.*?)\[\/row]/i', 'replaceRowXCallback', $str);


			$this->page_content = $str;
		}

		$this->page_date_update = time();

		return parent::beforeSave();
	}

	// ------------------------------------------------------------------------------------
}

class PageException extends Exception{}